package com.cf.r1032_ak;

import java.io.*;

public class c {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        in.nextToken(); int t = (int) in.nval;
        while (t-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static void solve() throws IOException {
        in.nextToken(); int n = (int) in.nval;
        in.nextToken(); int m = (int) in.nval;
        int max = -1;
        int[][] a = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                in.nextToken(); a[i][j] = (int) in.nval;
                max = Math.max(max, a[i][j]);
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j] == max) {
                    // 行
                    int r = -1, c = -1;
                    boolean f = true;
                    for (int x = 0; x < n && f; x++) {
                        for (int y = 0; y < m; y++) {
                            if (x == i) continue;
                            if (a[x][y] == max) {
                                if (c == -1) c = y;
                                else if (c != y) {
                                    f = false;
                                    break;
                                }
                            }
                        }
                    }
                    if (f) {
                        out.println(max - 1);
                        return;
                    }
                    f = true;
                    for (int x = 0; x < n && f; x++) {
                        for (int y = 0; y < m; y++) {
                            if (y == j) continue;
                            if (a[x][y] == max) {
                                if (r == -1) r = x;
                                else if (r != x) {
                                    f = false;
                                    break;
                                }
                            }
                        }
                    }
                    if (f) {
                        out.println(max - 1);
                        return;
                    }
                    out.println(max);
                    return;
                }
            }
        }
    }
}
